package com.zwh.spring.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zwh.spring.demo.mapper.primary.UserMapper;
import com.zwh.spring.demo.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	@Qualifier("primaryJdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public void create(String name, String password, Integer age) {
		jdbcTemplate.update("insert into user(name, password, age) values(?,?,?)", name, password, age);
	}

	@Override
	public void deleteByName(String name) {
		jdbcTemplate.update("delete from user where name = ?", name);
	}

	@Override
	public Integer getAllUsers() {

		return jdbcTemplate.queryForObject("select count(1) from user", Integer.class);
	}

	@Override
	public void deleteAllUsers() {
		//jdbcTemplate.update("delete from user");
		
		userMapper.deleteAll();
	}

	@Override
	@Transactional("primaryTransectionManager")
	public Integer add5Users() {
		
		userMapper.insertByParam("tom", "123456", 85);
		userMapper.insertByParam("jack", "123456", 115);
		userMapper.insertByParam("rose_exception", "123456", 122);
		userMapper.insertByParam("gelly", "123456", 77);
		userMapper.insertByParam("jackson", "123456", 34);
		
		return 5;
	}

}
